package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//列出客户端错误日志列表
func ListError(p ListErrorParams, isUnique int64) ([]orm.Params, int64, error) {
	where := "1=1"
	if p.Uid != 0 {
		where = where + fmt.Sprintf(" AND uid = %v ", p.Uid)
	}
	if p.StartTime != 0 && p.EndTime > p.StartTime {
		where = where + fmt.Sprintf(" AND error_time > %v AND error_time < %v ", p.StartTime, p.EndTime)
	}
	if p.Content != "" {
		where = where + fmt.Sprintf(" AND msg  LIKE '%v'", "%"+p.Content+"%")
	}
	if p.Model != "" {
		where = where + fmt.Sprintf(" AND model = '%v' ", p.Model)
	}
	if p.VersionId != "" {
		where = where + fmt.Sprintf(" AND version_id = '%v' ", p.VersionId)
	}
	if p.Status == 0 {
		where = where + fmt.Sprintf(" AND status = 0 ")
	} else {
		where = where + fmt.Sprintf(" AND status = 1 ")
	}
	limit := (p.PageIndex - 1) * p.PageSize
	var sql string
	var sqlCount string
	if isUnique == 0 {
		//去重
		sql = fmt.Sprintf(`
			SELECT
				COUNT(id) AS count,id,uid,msg,ip,
				device_id,model,version_id,error_time,hot_version,md5
			FROM
				%v
			WHERE
				%v
			GROUP BY
				md5 DESC
			ORDER BY
				id DESC
			LIMIT ?,?
		`, TableClientError, where)
		sqlCount = fmt.Sprintf(`
			SELECT
				id
			FROM
				%v
			WHERE
				%v
			GROUP BY
				md5
		`, TableClientError, where)
	} else {
		sql = fmt.Sprintf(`
			SELECT
				*
			FROM
				%v
			WHERE
				%v
			ORDER BY
				id
			DESC
			LIMIT ?,?
		`, TableClientError, where)
		sqlCount = fmt.Sprintf(`
			SELECT
				id
			FROM
				%v
			WHERE
				%v
		`, TableClientError, where)
	}
	o := NewDBOrm(DBChessLog)
	var errMsg []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&errMsg)
	if CheckError(err) {
		return errMsg, 0, err
	}
	var id []int64
	count, err := o.Raw(sqlCount).QueryRows(&id)
	if CheckError(err) {
		return errMsg, 0, err
	}
	return errMsg, count, nil
}

//更新错误日志的状态
func UpdateErrorStatus(tu map[string]interface{}, md5 string, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessLog)
	condition := fmt.Sprintf(`md5 = '%v'`, md5)
	values, sql := UpdateSql(TableClientError, tu, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//列出客户端提交的打点日志列表
func ListSubmitLog(p ListSubmitLogParams) ([]orm.Params, int64, error) {
	where := "1=1"
	if p.Uid != 0 {
		where = where + fmt.Sprintf(" AND uid = %v ", p.Uid)
	}
	if p.StartTime != 0 && p.EndTime > p.StartTime {
		where = where + fmt.Sprintf(" AND error_time > %v AND error_time < %v ", p.StartTime, p.EndTime)
	}
	if p.Content != "" {
		where = where + fmt.Sprintf(" AND msg  LIKE '%v'", "%"+p.Content+"%")
	}
	if p.Status != 0 {
		where = where + fmt.Sprintf(" AND status = %v", p.Status)
	}
	if p.Mod != 0 {
		where = where + fmt.Sprintf(" AND mod = %v", p.Mod)
	}
	if p.DeviceId != "" {
		where = where + fmt.Sprintf(" AND device_id = '%v'", p.DeviceId)
	}

	limit := (p.PageIndex - 1) * p.PageSize
	var sql string
	sql = fmt.Sprintf(`
			SELECT
				*
			FROM
				%v
			WHERE
				%v
			ORDER BY
				id
			DESC
			LIMIT ?,?
		`, TableSubmitLog, where)
	o := NewDBOrm(DBChessLog2)
	var errMsg []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).Values(&errMsg)
	if CheckError(err) {
		return errMsg, 0, err
	}
	var count int64
	sql = fmt.Sprintf(`
			SELECT
				COUNT(1)
			FROM
				%v
			WHERE
				%v
		`, TableSubmitLog, where)
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return errMsg, 0, err
	}
	return errMsg, count, nil
}

//统计客户端提交的打点日志列表
func StatSubmitLog(p ListSubmitLogParams) ([]orm.Params, error) {
	var errMsg []orm.Params
	if p.StartTime == 0 || p.EndTime == 0 {
		return append(errMsg, map[string]interface{}{
			"count":  0,
			"status": 0,
			"mod":    0,
		}), nil
	}
	where := "1=1"
	if p.Uid != 0 {
		where = where + fmt.Sprintf(" AND uid = %v ", p.Uid)
	}
	if p.StartTime != 0 && p.EndTime > p.StartTime {
		where = where + fmt.Sprintf(" AND error_time > %v AND error_time < %v ", p.StartTime, p.EndTime)
	}
	var sql string
	sql = fmt.Sprintf("SELECT COUNT(1) AS count,`mod`,`status` FROM %v WHERE %v GROUP BY `mod`,`status`",
		TableSubmitLog, where)
	o := NewDBOrm(DBChessLog2)
	_, err := o.Raw(sql).Values(&errMsg)
	return errMsg, err
}

//列出客户端错误日志列表
func ListErrorLog(p ListErrorLogParams) ([]*ErrorLog, int64, error) {
	where := "1=1"
	if p.Uid != 0 {
		where = where + fmt.Sprintf(" AND uid = %v ", p.Uid)
	}
	if p.StartTime != 0 && p.EndTime > p.StartTime {
		where = where + fmt.Sprintf(" AND error_time > %v AND error_time < %v ", p.StartTime, p.EndTime)
	}
	if p.Content != "" {
		where = where + fmt.Sprintf(" AND msg  LIKE '%v'", "%"+p.Content+"%")
	}
	if p.Model != "" {
		where = where + fmt.Sprintf(" AND model = '%v' ", p.Model)
	}
	if p.AppVersion != "" {
		where = where + fmt.Sprintf(" AND app_version = '%v' ", p.AppVersion)
	}
	if p.GameVersion != "" {
		where = where + fmt.Sprintf(" AND game_version = '%v' ", p.GameVersion)
	}
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			%v
		ORDER BY
			id
		DESC
		LIMIT ?,?
		`, TableClientOperationError, where)
	o := NewDBOrm(DBChessLog)
	var errMsg []*ErrorLog
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&errMsg)
	if CheckError(err) {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE %v`,
		TableClientOperationError, where)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return nil, 0, err
	}
	return errMsg, count, nil
}
